import { Component, OnInit, EventEmitter, Output } from '@angular/core';
import { NzModalService } from 'ng-zorro-antd/modal';
// 响应式表单
import { FormBuilder, FormControl, FormGroup, MaxLengthValidator, ValidationErrors, Validators } from '@angular/forms';
import { Observable, Observer } from 'rxjs';
import { Reward } from '../../../domains/reward.domain'
import { RewardServiceSvr } from '../../../services/reward.service'
import { EmployeeServiceSvr } from '../../../services/employee.service'

@Component({
  selector: 'app-reward-or-punish',
  templateUrl: './reward-or-punish.component.html',
  styleUrls: ['./reward-or-punish.component.less']
})

export class RewardOrPunishComponent implements OnInit {
  @Output() cancelDetailFileShowModal = new EventEmitter();

  reward:any
  EmpId:string
  // 日期
  date:any
  dateFormat = 'yyyy-MM-dd';

  // 新增数据
  addDate:any

  // 增加模态框
  isVisible = false;

  listOfData: any= [];

  constructor(
    private modalSvr: NzModalService,
    private addReward: RewardServiceSvr,
    private fb: FormBuilder,
    private employSvr: EmployeeServiceSvr,    
    ) { 
      // 增加 - 响应式表单
      this.validateForm = this.fb.group({
        time: [null],
        reason: [null, [Validators.required]],
        way: [null, [Validators.required ,this.numberValidator]]
      });
    }

  // 校验数字
  numberValidator(value):any{
    var myreg=/^\d+$/;
    let valid=myreg.test(value.value);
    return valid?null:{way:true};
  }
  
  // 增加
  showModal(): void {
    this.isVisible = true;
  }
  
  handleCancel(): void {
    console.log('Button cancel clicked!');
    this.isVisible = false;
  }

  // 增加的响应式表单
  validateForm: FormGroup;
  testId:number = 10;

  submitForm(value: { reason:string, way:any, time:any }): void {
    for (const key in this.validateForm.controls) {
      this.validateForm.controls[key].markAsDirty();
      this.validateForm.controls[key].updateValueAndValidity();
    }
    this.isVisible = false;
    this.date = new Date()
    this.date = this.date.getFullYear() + '-' + (this.date.getMonth() + 1) + '-' + this.date.getDate();
    this.testId ++;

    this.reward = new Reward({
      EmpId: this.EmpId,
      RewardMoney : value.way,
      RewardReason: value.reason,
      RewardTime: this.date
    })

    this.addReward.addReward(this.reward).then(res => {
      // 重新排序
      this.getAll()
    })
  }

  // 重置
  resetForm(e: MouseEvent): void {
    e.preventDefault();
    this.validateForm.reset();
    for (const key in this.validateForm.controls) {
      this.validateForm.controls[key].markAsPristine();
      this.validateForm.controls[key].updateValueAndValidity();
    }
  }

  // 返回上一级
  back(): void {
    this.cancelDetailFileShowModal.emit();
    window.sessionStorage.setItem('index', '-1');
  }

  // 获取所有奖惩信息
  getAll():void {
    this.employSvr.getArchivesByEmpId(this.EmpId).then(res => {
      this.listOfData = res.Rewards
    })
  }

  // 获取所有最新消息
  ngOnInit(): void {
    // 初始化
    this.EmpId = window.sessionStorage.getItem('sendEnterFilesID');
    this.getAll()
  }

}


